package QuanLyNguoiDung;

import KetNoiSQL.KetNoiSQL;
import KiemTra.KiemTra;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class DanhSachNguoiDung extends javax.swing.JPanel {

    private DefaultTableModel modelLoaiNSD = new DefaultTableModel();
    private DefaultTableModel modelNSD = new DefaultTableModel();
    Vector vLay = new Vector();
    NguoiDung nd = new NguoiDung();

    public DanhSachNguoiDung() {
        initComponents();
        try {
          
            vLay = NguoiDung.xemLoaiNSD();
            this.taiBangLoaiNSD(vLay);

            vLay = NguoiDung.xemNguoiSuDung();
            this.taiBangNSD(vLay);
            
             vLay = NguoiDung.xemLoaiNSD();
            layTenQuyenNguoiDung(vLay);
        } catch (SQLException ex) {
            Logger.getLogger(DanhSachNguoiDung.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    //Tải dữ liệu vào bảng Loại người sử dụng
    public void taiBangLoaiNSD(Vector vLay) throws SQLException {
        modelLoaiNSD.setRowCount(0);
        Vector cotLoaiNSD = new Vector();
        cotLoaiNSD.addElement("Mã");
        cotLoaiNSD.addElement("Tên loại NSD");
        modelLoaiNSD.setColumnIdentifiers(cotLoaiNSD);
        NguoiDung nd = new NguoiDung();
        for (int i = 0; i < vLay.size(); i++) {
            nd = (NguoiDung) vLay.get(i);
            Vector hangLoaiNSD = new Vector();
            hangLoaiNSD.addElement(nd.getMaLoaiNSD());
            hangLoaiNSD.addElement(nd.getTenLoaiNSD());
            modelLoaiNSD.addRow(hangLoaiNSD);
        }
        tbLoaiNSD.setModel(modelLoaiNSD);
    }

    //Kiểm tra mã Loại người dùng
    public boolean ktraMaLoaiNguoiDung_Them() {
        String ten = txtMaLoaiNSD.getText().trim();
        boolean kt = false;
        try {
            ResultSet rs = KetNoiSQL.thucHienTruVan("spXemMaLoaiNSD");
            while (rs.next()) {
                if (ten.equalsIgnoreCase(rs.getString("maLoaiNSD"))) {
                    txtMaLoaiNSD.setText("");
                    txtMaLoaiNSD.requestFocus();
                    kt = true;
                }
            }
            if (kt == true) {
                JOptionPane.showMessageDialog(this, "Mã loại người dùng đã tồn tại !", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return kt;
    }
    //Kiểm tra mã Loại người dùng

    public boolean ktraMaLoaiNguoiDung_CapNhat() {
        String ten = txtMaLoaiNSD.getText().trim();
        boolean kt = false;
        try {
            ResultSet rs = KetNoiSQL.thucHienTruVan("spXemMaLoaiNSD");
            while (rs.next()) {
                if (ten.equalsIgnoreCase(rs.getString("maLoaiNSD"))) {
                    txtMaLoaiNSD.requestFocus();
                    kt = true;
                }
            }
            if (kt == false) {
                JOptionPane.showMessageDialog(this, "Mã loại người dùng không tồn tại!Xem lại?", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return kt;
    }

    //Kiểm tra loại người dùng
    public boolean ktraLoaiNguoiDung() {
        int kt = 0;
        KiemTra ktra = new KiemTra();
        if (txtMaLoaiNSD.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Bạn chưa nhập mã người dùng", "Thông báo", JOptionPane.ERROR_MESSAGE);
            txtMaLoaiNSD.requestFocus();
            return false;
        }
        if (txtTenLoaiNSD.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Bạn chưa nhập tên người dùng", "Thông báo", JOptionPane.ERROR_MESSAGE);
            txtTenLoaiNSD.requestFocus();
            return false;
        }
        return true;
    }
    //Thêm loại thuốc mới

    public void themLoaiThuoc() {
        if (!ktraLoaiNguoiDung()) {
            return;
        }

        if (ktraMaLoaiNguoiDung_Them()) {
            return;
        }
        int kt = 0;
        NguoiDung nd = new NguoiDung();
        nd.setMaLoaiNSD(txtMaLoaiNSD.getText().trim());
        nd.setTenLoaiNSD(txtTenLoaiNSD.getText().trim());
        try {
            kt = NguoiDung.themLoaiNguoiDung(nd);
            if (kt == 1) {
                JOptionPane.showMessageDialog(this, "Thêm mới loại người dùng thành công !", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                txtMaLoaiNSD.setText("");
                txtTenLoaiNSD.setText("");
                vLay = NguoiDung.xemLoaiNSD();
                this.taiBangLoaiNSD(vLay);
            } else {
                JOptionPane.showMessageDialog(this, "Thêm mới loại người dùng không thành công.Xem lại", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    //Cập nhật loại người dùng
    public void capNhatLoaiNguoiDung() {
        int kt = 0;
        int r = tbLoaiNSD.getSelectedRow();
        if (r < 0) {
            JOptionPane.showMessageDialog(this, "Bạn phải chọn mục để cập nhật!", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (!ktraMaLoaiNguoiDung_CapNhat()) {
            return;
        }
        NguoiDung nd = new NguoiDung();
        nd.setMaLoaiNSD(txtMaLoaiNSD.getText().trim());
        nd.setTenLoaiNSD(txtTenLoaiNSD.getText().trim());
        try {
            kt = NguoiDung.capNhatLoaiNguoiDung(nd);
            if (kt == 1) {
                JOptionPane.showMessageDialog(this, "Cập nhật thành công!", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                txtMaLoaiNSD.setText("");
                txtTenLoaiNSD.setText("");
                vLay = NguoiDung.xemLoaiNSD();
                this.taiBangLoaiNSD(vLay);
            } else {
                JOptionPane.showMessageDialog(this, "Cập nhật bị lỗi.Xem lại!", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    //Xóa loại người dùng

    public void xoaLoaiNguoiDung() {
        int show = JOptionPane.showConfirmDialog(this, "Bạn có thực sự muốn xóa loại người dùng ?", "Thông báo xóa", JOptionPane.YES_NO_OPTION);
        if (show == JOptionPane.YES_OPTION) {
            int kt = 0, dem = 0;
            int[] rArr = tbLoaiNSD.getSelectedRows();
            try {
                for (int i = 0; i < rArr.length; i++) {
                    kt = NguoiDung.xoaLoaiNguoiDung(tbLoaiNSD.getValueAt(rArr[i], 0).toString());
                    if (kt == 1) {
                        dem++;
                    }
                }
                if (dem > 0) {
                    JOptionPane.showMessageDialog(this, "Xóa " + dem + " loai người dùng thành công", "Thông báo xóa", JOptionPane.INFORMATION_MESSAGE);
                    txtMaLoaiNSD.setText("");
                    txtTenLoaiNSD.setText("");
                    vLay = NguoiDung.xemLoaiNSD();
                    this.taiBangLoaiNSD(vLay);
                } else {
                    JOptionPane.showMessageDialog(this, "Xóa loại người dùng bị lỗi  !", "Thông báo xóa", JOptionPane.ERROR_MESSAGE);
                }
            } catch (SQLException ex) {
                System.err.println(ex.getMessage());
            }
        }
    }
    //--------------------Danh sách người dùng-----------------------------------------------------------------------------------------------

    //Tải dữ liệu vào bảng người sử dụng
    public void taiBangNSD(Vector vLay) throws SQLException {
        modelNSD.setRowCount(0);
        Vector cotLoaiNSD = new Vector();
        cotLoaiNSD.addElement("Mã");
        cotLoaiNSD.addElement("Tên đăng nhập");
        cotLoaiNSD.addElement("Tên đầy đủ");
        cotLoaiNSD.addElement("Loại NSD");
        cotLoaiNSD.addElement("Địa chỉ");
        cotLoaiNSD.addElement("SDT");
        cotLoaiNSD.addElement("Email");
        modelNSD.setColumnIdentifiers(cotLoaiNSD);

        NguoiDung nd = new NguoiDung();
        for (int i = 0; i < vLay.size(); i++) {
            nd = (NguoiDung) vLay.get(i);
            Vector hangLoaiNSD = new Vector();
            hangLoaiNSD.addElement(nd.getMaNSD());
            hangLoaiNSD.addElement(nd.getTenDangNhap());
            hangLoaiNSD.addElement(nd.getTenDayDu());
            hangLoaiNSD.addElement(nd.getTenLoaiNSD());
            hangLoaiNSD.addElement(nd.getDiaChi());
            hangLoaiNSD.addElement(nd.getSDT());
            hangLoaiNSD.addElement(nd.getEmail());
            modelNSD.addRow(hangLoaiNSD);
        }
        tbDanhSachNSD.setModel(modelNSD);
    }

//CẬP NHẬT NGƯỜI SỬ DỤNG
    public void capNhatDanhSachNguoiDung() {
        int kt = 0;
        int r = tbDanhSachNSD.getSelectedRow();
        if (r < 0) {
            JOptionPane.showMessageDialog(this, "Bạn chưa chọn mục để cập nhật", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return;
        }
        NguoiDung nd = new NguoiDung();

        int maNSD = Integer.parseInt(tbDanhSachNSD.getValueAt(r, 0).toString().trim());
        nd.setMaNSD(maNSD);
        nd.setTenDangNhap(tbDanhSachNSD.getValueAt(r, 1).toString().trim());
        nd.setTenDayDu(tbDanhSachNSD.getValueAt(r, 2).toString().trim());

        //Lấy quyền người sử dụng
        Vector vLoaiNguoiDung = new Vector();
        try {
            vLoaiNguoiDung = NguoiDung.xemLoaiNSD();
        } catch (SQLException ex) {
            Logger.getLogger(ThemNguoiSuDung.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (int i = 0; i < vLoaiNguoiDung.size(); i++) {
            NguoiDung nd1 = new NguoiDung();
            nd1 = (NguoiDung) vLoaiNguoiDung.get(i);
            //so sánh tên trùng thì lấy ra mã loại người sử dụng
            if (tbDanhSachNSD.getValueAt(r, 3).toString().trim().equals(nd1.getTenLoaiNSD().toString())) {
                nd.setMaLoaiNSD(nd1.getMaLoaiNSD());
            }
        }

        nd.setDiaChi(tbDanhSachNSD.getValueAt(r, 4).toString().trim());
        nd.setSDT(tbDanhSachNSD.getValueAt(r, 5).toString().trim());
        nd.setEmail(tbDanhSachNSD.getValueAt(r, 6).toString().trim());

        try {
            kt = NguoiDung.capNhatNguoiDung(nd);
            if (kt == 1) {
                JOptionPane.showMessageDialog(this, "Cập nhật thành công  !", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                vLay = NguoiDung.xemNguoiSuDung();
                this.taiBangNSD(vLay);
            } else {
                JOptionPane.showMessageDialog(this, "Cập nhật bị lỗi.Xem lại", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    //XÓA NGƯỜI SỬ DỤNG
    public void xoaNguoiSuDung() {
        int r = tbDanhSachNSD.getSelectedRow();
        if (r < 0) {
            JOptionPane.showMessageDialog(this, "Bạn chưa chọn mục để xóa", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return;
        }
        int show = JOptionPane.showConfirmDialog(this, "Bạn có đồng ý xóa dữ liệu người sử dụng ?", "Thông báo", JOptionPane.YES_NO_OPTION);
        if (show == JOptionPane.YES_OPTION) {
            int kt = 0, dem = 0;
            int[] rArr = tbDanhSachNSD.getSelectedRows();
            try {
                for (int i = 0; i < rArr.length; i++) {
                    kt = NguoiDung.xoaNguoiDung(tbDanhSachNSD.getValueAt(rArr[i], 1).toString());
                    if (kt == 1) {
                        dem++;
                    }
                }
                if (dem > 0) {
                    JOptionPane.showMessageDialog(this, "Xóa " + dem + " Người sử dụng thành công", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                    vLay = NguoiDung.xemNguoiSuDung();
                    this.taiBangNSD(vLay);
                } else {
                    JOptionPane.showMessageDialog(this, "Không xóa được!", "Thông báo", JOptionPane.ERROR_MESSAGE);
                }
            } catch (SQLException ex) {
                System.err.println(ex.getMessage());
            }
        }
    }

    //Lấy quyền sử dụng vào combobox
    public void layTenQuyenNguoiDung(Vector vLay) {
        Vector loaiNguoiDung = new Vector();
        loaiNguoiDung.addElement("---Chọn quyền người dùng---");
        for (int i = 0; i < vLay.size(); i++) {
            nd = (NguoiDung) vLay.get(i);

            loaiNguoiDung.addElement(nd.getTenLoaiNSD());
        }
        DefaultComboBoxModel modelLoaiNguoiDung = new DefaultComboBoxModel(loaiNguoiDung);
        cbLoaiNSD.setModel(modelLoaiNguoiDung);
    }
    //Tìm kiếm Người dùng-----------------------------------------------------------------------------------

    public void timKiemNguoiDung() throws SQLException {
         Vector vlist = new Vector();
        if (txtTenDangNhap.getText().equals("") && cbLoaiNSD.getSelectedItem().toString().equals("---Chọn quyền người dùng---")){
            JOptionPane.showMessageDialog(null, "Bạn phải chọn ít nhất một mục để tìm kiếm");
            return;
        }
        else if (txtTenDangNhap.getText() != "" && cbLoaiNSD.getSelectedItem().toString().equals("---Chọn quyền người dùng---")) {
            vlist.addElement(txtTenDangNhap.getText().trim());
            vLay = NguoiDung.timKiem("spTimKiemTenDangNhap", vlist);
            this.taiBangNSD(vLay);
        }else if (txtTenDangNhap.getText().equals("") && cbLoaiNSD.getSelectedItem().toString()!=("---Chọn quyền người dùng---")) {
            vlist.addElement(cbLoaiNSD.getSelectedItem().toString().trim());
            vLay = NguoiDung.timKiem("spTimKiemQuyenNSD", vlist);
            this.taiBangNSD(vLay);
        } else {
            vlist.addElement(txtTenDangNhap.getText().trim());
            vlist.addElement(cbLoaiNSD.getSelectedItem().toString().trim());
            vLay = NguoiDung.timKiem("spTimKiemTenDangNhap_QuyenNSD", vlist);
            this.taiBangNSD(vLay);
        }

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDanhSachNSD = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbLoaiNSD = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        txtTenLoaiNSD = new javax.swing.JTextField();
        btThemLNSD = new javax.swing.JButton();
        btCNLNSD = new javax.swing.JButton();
        btXoaLoaiNSD = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txtMaLoaiNSD = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtTenDangNhap = new javax.swing.JTextField();
        cbLoaiNSD = new javax.swing.JComboBox();
        btTimKiem = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        btDoiMatKhau = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        btTroVe = new javax.swing.JButton();
        btXoa = new javax.swing.JButton();
        btCapNhat = new javax.swing.JButton();
        btThem = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách người dùng"));

        tbDanhSachNSD.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbDanhSachNSD.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
        tbDanhSachNSD.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDanhSachNSDMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbDanhSachNSD);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Loại người sử dụng"));

        tbLoaiNSD.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbLoaiNSD.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbLoaiNSDMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tbLoaiNSD);

        jLabel1.setText("Tên Loai NSD");

        btThemLNSD.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/add 1.png"))); // NOI18N
        btThemLNSD.setText("Thêm Loại NSD");
        btThemLNSD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThemLNSDActionPerformed(evt);
            }
        });

        btCNLNSD.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/update.png"))); // NOI18N
        btCNLNSD.setText("Cập nhật Loại NSD");
        btCNLNSD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCNLNSDActionPerformed(evt);
            }
        });

        btXoaLoaiNSD.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/cross.png"))); // NOI18N
        btXoaLoaiNSD.setText("Xóa");
        btXoaLoaiNSD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btXoaLoaiNSDActionPerformed(evt);
            }
        });

        jLabel3.setText("Mã Loại NSD");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtMaLoaiNSD))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtTenLoaiNSD, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 46, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addComponent(btThemLNSD)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btXoaLoaiNSD, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(59, 59, 59)
                .addComponent(btCNLNSD)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel3});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtMaLoaiNSD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTenLoaiNSD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btThemLNSD, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btXoaLoaiNSD, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btCNLNSD, javax.swing.GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE)
                .addGap(25, 25, 25))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Tìm kiếm"));

        jLabel2.setText("Nhập tên đăng nhập");

        cbLoaiNSD.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        btTimKiem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/Search.png"))); // NOI18N
        btTimKiem.setText("Tìm kiếm");
        btTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTimKiemActionPerformed(evt);
            }
        });

        jLabel4.setText("Chọn loại NSD");

        btDoiMatKhau.setText("Đổi Mật Khẩu");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(btTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(53, 53, 53)
                        .addComponent(btDoiMatKhau, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(31, 31, 31)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cbLoaiNSD, 0, 183, Short.MAX_VALUE)
                            .addComponent(txtTenDangNhap))))
                .addContainerGap(56, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTenDangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cbLoaiNSD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                        .addGap(3, 3, 3)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btDoiMatKhau, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        btTroVe.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/Go-back.png"))); // NOI18N
        btTroVe.setText("Trở về");
        btTroVe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTroVeActionPerformed(evt);
            }
        });

        btXoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/cross.png"))); // NOI18N
        btXoa.setText("Xóa");
        btXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btXoaActionPerformed(evt);
            }
        });

        btCapNhat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/update.png"))); // NOI18N
        btCapNhat.setText("Cập nhật");
        btCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCapNhatActionPerformed(evt);
            }
        });

        btThem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/add 1.png"))); // NOI18N
        btThem.setText("Thêm");
        btThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btXoa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btThem, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btCapNhat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btTroVe, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btCapNhat, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btThem, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btTroVe, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25))
        );

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/ngouidung.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(86, 86, 86))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(24, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btCNLNSDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCNLNSDActionPerformed
        // TODO add your handling code here:
        this.capNhatLoaiNguoiDung();
    }//GEN-LAST:event_btCNLNSDActionPerformed

    private void btTroVeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTroVeActionPerformed
        try {
            // TODO add your handling code here:
            txtMaLoaiNSD.setText("");
            txtTenDangNhap.setText("");
            txtTenLoaiNSD.setText("");
            cbLoaiNSD.setSelectedIndex(0);
            vLay = NguoiDung.xemNguoiSuDung();
                this.taiBangNSD(vLay);
        } catch (SQLException ex) {
            Logger.getLogger(DanhSachNguoiDung.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btTroVeActionPerformed

    private void btXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXoaActionPerformed
        // TODO add your handling code here:
        this.xoaNguoiSuDung();
    }//GEN-LAST:event_btXoaActionPerformed

    private void btThemLNSDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThemLNSDActionPerformed
        // TODO add your handling code here:
        this.themLoaiThuoc();

    }//GEN-LAST:event_btThemLNSDActionPerformed

    private void btXoaLoaiNSDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXoaLoaiNSDActionPerformed
        // TODO add your handling code here:
        this.xoaLoaiNguoiDung();
    }//GEN-LAST:event_btXoaLoaiNSDActionPerformed

    private void tbDanhSachNSDMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDanhSachNSDMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_tbDanhSachNSDMouseClicked

    private void tbLoaiNSDMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbLoaiNSDMouseClicked
        // TODO add your handling code here:
        int r = tbLoaiNSD.getSelectedRow();
        if (r < 0) {
            return;
        }
        txtMaLoaiNSD.setText("" + tbLoaiNSD.getValueAt(r, 0));
        txtTenLoaiNSD.setText("" + tbLoaiNSD.getValueAt(r, 1));
    }//GEN-LAST:event_tbLoaiNSDMouseClicked

    private void btThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThemActionPerformed
        // TODO add your handling code here:
        new ThemNguoiSuDung().setVisible(true);
    }//GEN-LAST:event_btThemActionPerformed

    private void btCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCapNhatActionPerformed
        // TODO add your handling code here:
        this.capNhatDanhSachNguoiDung();
    }//GEN-LAST:event_btCapNhatActionPerformed

    private void btTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTimKiemActionPerformed
        try {
            // TODO add your handling code here:
            this.timKiemNguoiDung();
        } catch (SQLException ex) {
            Logger.getLogger(DanhSachNguoiDung.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btTimKiemActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCNLNSD;
    private javax.swing.JButton btCapNhat;
    private javax.swing.JButton btDoiMatKhau;
    private javax.swing.JButton btThem;
    private javax.swing.JButton btThemLNSD;
    private javax.swing.JButton btTimKiem;
    private javax.swing.JButton btTroVe;
    private javax.swing.JButton btXoa;
    private javax.swing.JButton btXoaLoaiNSD;
    private javax.swing.JComboBox cbLoaiNSD;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tbDanhSachNSD;
    private javax.swing.JTable tbLoaiNSD;
    private javax.swing.JTextField txtMaLoaiNSD;
    private javax.swing.JTextField txtTenDangNhap;
    private javax.swing.JTextField txtTenLoaiNSD;
    // End of variables declaration//GEN-END:variables
}
